package com.yfj.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yfj.entity.People;
import com.yfj.service.IndexService;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController {
	
	@Autowired
	IndexService indexService;
	
	@RequestMapping({"/","/index"})
	public String index() {
		return "index";
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public Map<String,Object> login(Long id, String password, String identity) {
		Map<String,Object> m=new HashMap<String,Object>();
		People p = indexService.login(id, identity);

		//shiro认证
		Subject subject= SecurityUtils.getSubject();
		String username=Long.toString(id);
		UsernamePasswordToken token=new UsernamePasswordToken(username,password);


			m.put("mark",0);
		try {
			subject.login(token);
			m.put("mark", 1 );
			m.put("name", p.getName());
			m.put("id", p.getId());
			m.put("college",p.getCollege() );
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return m;
		}
		return m;
	}



	@RequestMapping("/403")
	public String noPermission(){
		return "403";
	}
	
	
}
